package com.cidp.system.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;

import com.cidp.system.model.Tsysteminfo;
import com.cidp.system.pojo.SysteminfoPo;
import com.cidp.system.service.ISystemInfoService;


/**
 * <b>标题：</b>系统信息.
 * <pre><b>描述</b>
 *    系统信息管理
 * </pre>
 * 
 * @author lijian
 * @since 2008-6-3
 * @version 1.0
 */
public class SysteminfoAction extends DispatchAction{
	
	private ISystemInfoService systemInfoService;
	
	/**
	 * 查看系统信息
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward search(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		SysteminfoPo systeminfoPo = (SysteminfoPo)dynaActionForm.get("systeminfoPo");
		
		Tsysteminfo systeminfo = systemInfoService.getTsysteminfo();
		try{
			BeanUtils.copyProperties(systeminfoPo, systeminfo);
		}catch(Exception e){
			
		}
		dynaActionForm.set("systeminfoPo", systeminfoPo);
		
		return mapping.findForward("toList");
	}
	
	/**
	 * 更新系统信息
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward update(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		SysteminfoPo systeminfoPo = (SysteminfoPo)dynaActionForm.get("SysteminfoPo");
		
		Tsysteminfo systeminfo = systemInfoService.loadById(systeminfoPo.getSystemId());
		systeminfo.setCompanyName(systeminfoPo.getCompanyName());
		systeminfo.setCopyright(systeminfoPo.getCopyright());
		systeminfo.setLastUpdateDatetime(new Date());
		systeminfo.setSmtpAuthFlag(systeminfoPo.getSmtpAuthFlag());
		if(systeminfoPo.getSmtpAuthFlag().equals(new Long(1))){
			systeminfo.setSmtpPass(systeminfoPo.getSmtpPass());
			systeminfo.setSmtpPort(systeminfoPo.getSmtpPort());
		}
		systeminfo.setSmtpServer(systeminfoPo.getSmtpServer());
		systeminfo.setSmtpSslFlag(systeminfoPo.getSmtpSslFlag());
		systeminfo.setSmtpUser(systeminfoPo.getSmtpUser());
		systeminfo.setSystemCode(systeminfoPo.getSystemCode());
		systeminfo.setSystemEmail(systeminfoPo.getSystemEmail());
		systeminfo.setSystemEmailSign(systeminfoPo.getSystemEmailSign());
		systeminfo.setSystemName(systeminfoPo.getSystemName());
		systeminfo.setSystemVersion(systeminfoPo.getSystemVersion());
		
		systemInfoService.updateSystemInfo(systeminfo);
		
		return mapping.findForward("toList");
	}

	public void setSystemInfoService(ISystemInfoService systemInfoService) {
		this.systemInfoService = systemInfoService;
	}
}
